<?php
/**
 * @title 777. 在LR字符串中交换相邻字符 - 逐位比较
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $start
     * @param String $end
     * @return Boolean
     */
    function canTransform($start, $end) {
        /**
         * @since 2020-09-11 字符统计
         */
        $count1 = self::charCount($start);
        $count2 = self::charCount($end);
        return [$count1, $count2];

        /**
         * @since 2020-09-11 对比
         */
        $len1 = count($count1);
        $len2 = count($count2);

        /**
         * @since 2020-09-11 对比
         */
        if($len1 != $len2){
            return false;
        } else {
            $x = 0;
            for($i=0; $i<$len1; $i++){
                if($count1[$i][0] == $count2[$i][0] AND $count1[$i][2] == $count2[$i][2]){
                    $x = $count1[$i][1] - $count2[$i][1] + $x;
                } else {
                    return false;
                }
            }

            if($x == 0){
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * @title 字符统计
     * @author start2004
     * @since 2020-09-11 Created
     *
     * @param array $start
     */
    public function charCount($start = []){
        /**
         * @since 2020-09-11 长度
         */
        $len = strlen($start);

        /**
         * @since 2020-09-11 字符区间
         */
        $char = null;

        /**
         * @since 2020-09-11 数量
         */
        $x = $l = $r = 0;

        /**
         * @since 2020-09-11 统计数组
         */
        $countArray = [];

        /**
         * @since 2020-09-11 统计数量
         */
        for($i=0; $i<$len; $i++){
            if($start[$i] == "X"){
                $x++;
            } elseif($start[$i] == "L"){
                /**
                 * @since 2020-09-11 char=L OR null
                 */
                if($char != "R"){
                    $char = "L";
                    $l++;
                } else {
                    /**
                     * @since 2020-09-11 char=R
                     * @since 2020-09-11 入库
                     */
                    $countArray[] = [$l, $x, $r];

                    /**
                     * @since 2020-09-11 重置次数
                     */
                    $char = "L";
                    $l = 1;
                    $x = 0;
                    $r = 0;
                }
            } else {
                /**
                 * @since 2020-09-11 start[i] = R
                 * @since 2020-09-11 char=R
                 */
                if($char != "L"){
                    $char = "R";
                    $r++;
                } else {
                    /**
                     * @since 2020-09-11 char=L
                     * @since 2020-09-11 入库
                     */
                    $countArray[] = [$l, $x, $r];

                    /**
                     * @since 2020-09-11 重置次数
                     */
                    $char = "R";
                    $l = 0;
                    $x = 0;
                    $r = 1;
                }
            }

            // echo "start[i]=", $start[$i], ", [l,x,r] = [{$l}, {$x}, {$r}]", PHP_EOL;
        }

        /**
         * @since 2020-09-11 收官
         */
        $countArray[] = [$l, $x, $r];

        /**
         * @return
         */
        return $countArray;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202009/2020.09.10_8.php
 */
$datas = [
    ["LXXLXRLXXL", "XLLXRXLXLX"], // false
    // ["RXXLRXRXL", "XRLXXRRLX"],
    // ["XXXLXXXXXX", "XXXLXXXXXX"],
    // ["XXRXXXXRXXXXXXRXXXXLXXXXLXLXXXXXXRXXXXLX", "XXXXXXXXXXXXRRRLLLXXXXXXXXXXXXXXXXXXRLXX"],
    // ["XXXXXLXXXX", "LXXXXXXXXX"],
    // ["L", "X"],
    // ["L", "R"],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->canTransform(...$data);
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". $data[1][$i][0] ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
